<template>
  <div class="password-reset-container">
    <el-card class="password-reset-card">
      <div class="reset-password-title">修改密码</div>
      <el-form ref="passwordResetForm" :model="passwordResetForm" :rules="rules" label-width="120px">
        <el-form-item label="邮箱" prop="email">
          <el-col :span="24">
            <el-input v-model="passwordResetForm.email" placeholder="请输入邮箱"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-col :span="24">
            <el-row >
              <el-col :span="13">
                <el-input v-model="passwordResetForm.code" placeholder="请输入验证码"></el-input>
              </el-col>
              <el-col :span="10">
                <el-button type="primary" class="send-code-button" @click="sendCode" :disabled="sendCodeDisabled">{{ sendCodeDisabled ? '发送中...' : '获取验证码' }}</el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-col :span="24">
            <el-input type="password" v-model="passwordResetForm.password" placeholder="请输入新密码"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-col :span="24">
            <el-input type="password" v-model="passwordResetForm.confirmPassword" placeholder="请再次输入新密码"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-col :span="24">
            <el-button type="primary" @click="submitForm">修改密码</el-button>
            <el-button type="text" @click="goToLogin">登录</el-button>
          </el-col>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'PasswordReset',
  data() {
    return {
      passwordResetForm: {
        email: '',
        code: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: this.confirmPasswordValidator, trigger: 'blur' }
        ]
      },
      sendCodeDisabled: false,
      countDown: 0
    };
  },
  methods: {
    sendCode() {
      if (!this.passwordResetForm.email) {
        this.$message.error('请先输入邮箱');
        return;
      }
      const url = 'http://localhost:8887/admin/sendCode';
      axios.get(url, {
        params: {
          email: this.passwordResetForm.email
        }
      }).then(response => {
        if (response.data.code === 200) {
          this.$message.success('验证码已发送');
          this.startCountDown();
        } else {
          this.$message.error(response.data.message);
        }
      }).catch(error => {
        console.error('发送验证码失败:', error);
        this.$message.error('发送验证码失败，请检查网络或联系管理员');
      });
    },
    startCountDown() {
      this.sendCodeDisabled = true;
      let seconds = 60;
      this.countDown = seconds;
      const intervalId = window.setInterval(() => {
        seconds -= 1;
        this.countDown = seconds;
        if (seconds <= 0) {
          window.clearInterval(intervalId);
          this.sendCodeDisabled = false;
          this.countDown = 0;
        }
      }, 1000);
    },
    confirmPasswordValidator(rule, value, callback) {
      if (value !== this.passwordResetForm.password) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    },
    submitForm() {
      this.$refs.passwordResetForm.validate((valid) => {
        if (valid) {
          const data = {
            email: this.passwordResetForm.email,
            verificationCode: this.passwordResetForm.code,
            password: this.passwordResetForm.password
          };
          axios.post('http://localhost:8887/admin/updatePassword', data)
            .then(response => {
              if (response.data.code === 200) {
                this.$message.success('密码修改成功');
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch(error => {
              console.error('修改密码失败:', error);
              this.$message.error('修改密码失败，请检查网络或联系管理员');
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    goToLogin() {
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
.password-reset-container {
  background-image: url('@/assets/background.png');
  background-size: cover;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.password-reset-card {
  width: 400px;
}
.reset-password-title {
  text-align: center;
  font-size: 18px;
  margin-bottom: 20px;
}
.send-code-button {
  margin-left: 10px;
}
</style>